<template>
    <BasicModal
      v-bind="$attrs"
      @register="registerModal"
      show-footer
      width="60%"
      @ok="handleSubmit"
    >
      <template #title>
        {{ isUpdate ? '编辑' : '新增' }}
      </template>
      <template #footer>
        <a-button @click="closeModal" :loading="btnLoading">取消</a-button>
        <a-button
          type="primary"
          @click="handleSubmit(false)"
          :loading="btnLoading"
          >保存</a-button
        >
      </template>
      <BasicForm @register="registerForm">
        <template #positionSelect="{model}">
          <MapSelect :poi="[model.lng, model.lat]" @change="([lng, lat]) => handleSetPoi(model, lng, lat)"/>
        </template>
        <template #areaSelect="{model}">
          <AreaSelect v-model:modelValue="model.areaId"></AreaSelect>
        </template>
      </BasicForm>
    </BasicModal>
  </template>
  <script lang="ts" setup>
  import { ref } from 'vue';
  import { BasicModal, useModalInner } from '@/components/Modal';
  import { BasicForm, useForm } from '@/components/Form/index';
  import { useMessage } from '@/hooks/web/useMessage';
  import { useThrottleFn } from '@vueuse/core';
  import { defHttp } from '@/utils/http/axios';
  import MapSelect from './components/mapSelect.vue';
  import AreaSelect from './components/areaSelect.vue';
  const emit = defineEmits(['success', 'register']);
  let isUpdate = ref(true);
  const btnLoading = ref(true);
  const { createMessage } = useMessage();
  const [
    registerForm,
    { resetFields, setFieldsValue, validate },
  ] = useForm({
    labelWidth: 150,
    baseColProps: { span: 12 },
    schemas: [
      // 标题 展示图 位置
      {
        field: 'id',
        component: 'Input',
        label: 'id',
        show: false,
      },
      {
        field: 'name',
        component: 'Input',
        label: '名称',
        required: true,
      },
      {
        field: 'isOpen',
        component: 'RadioGroup',
        label: '是否列出',
        required: true,
        componentProps: {
          options: [
            { label: '是', value: 1 },
            { label: '否', value: 0 },
          ],
        }
      },
    
      {
        field: 'serviceStartTime',
        component: 'TimePicker',
        label: '营业开始时间',
        required: true,
        componentProps: {
          style: { width: '100%' },
          format: 'HH:mm',
          valueFormat: 'HH:mm',
        }
      },
      {
        field: 'serviceEndTime',
        component: 'TimePicker',
        label: '营业结束时间',
        required: true,
        componentProps: {
          style: { width: '100%' },
          format: 'HH:mm',
          valueFormat: 'HH:mm',
        }
      },
      {
        field: 'chargeStandardId',
        component: 'ApiSelect',
        label: '默认收费标准',
        required: true,
        componentProps: {
          api: '/admin/main/chargeStandardList',
          labelField: 'name',
          valueField: 'id',
          showSearch: true,
          allowClear: true,
        }
      },
      {
        field: 'mpChargeStandardId',
        component: 'ApiSelect',
        label: '小程序收费标准',
        required: true,
        componentProps: {
          api: '/admin/main/chargeStandardList',
          labelField: 'name',
          valueField: 'id',
          showSearch: true,
          allowClear: true,
        }
      },
      {
        field: 'org',
        component: 'Select',
        label: '所属企业',
        required: true,
        defaultValue: '1',
        componentProps: {
          options: [
            { label: '智电云智能充电', value: '1' }
          ]
        }
      },
      {
        field: 'memo',
        component: 'Input',
        label: '描述',
        required: true,
      },
      {
        field: 'parkMemo',
        component: 'Input',
        label: '停车费提示',
        required: true,
      },
      {
        field: 'isRcm',
        component: 'RadioGroup',
        label: '是否推荐',
        required: true,
        componentProps: {
          options: [
            { label: '是', value: '1' },
            { label: '否', value: '0' },
          ],
        }
      },
      {
        field: 'areaId',
        component: 'Input',
        label: '城市',
        required: true,
        slot: 'areaSelect',
      },
      {
        field: 'lat',
        component: 'Input',
        label: '经度',
        required: true,
      },
      {
        field: 'lng',
        component: 'Input',
        label: '纬度',
        required: true,
      },
      {
        field: 'positionSelect',
        component: 'Input',
        label: '位置',
        slot: 'positionSelect',
        colProps: {
          span: 24
        },
      },
      {
        field: 'serviceList',
        component: 'ApiCheckboxGroup',
        label: '标签',
        componentProps: {
          api: '/admin/main/serviceItemList',
          labelField: 'name',
          valueField: 'id',
        },
        colProps: {
          span: 24
        },
      },
    ],
    showActionButtonGroup: false,
  });
  const handleSetPoi = (formModel, lng, lat) => {
    formModel.lat = lat;
    formModel.lng = lng;
  }
  const getTags = async (id) => { 
    const res = await defHttp.get({ url: `/admin/station/stationServiceItems/${id}` });
    console.log(res);
    return res.map((item) => item.serviceItemId)
  }
  const [registerModal, { closeModal, changeLoading }] =
  useModalInner(async (data) => {
      resetFields();
      changeLoading(true);
      btnLoading.value = true;
      isUpdate.value =!!data?.isUpdate;
      if (isUpdate.value) {
        const id = data.record.id;
        const tags = await getTags(id);
        setFieldsValue({ ...data.record, serviceList: tags });
      }
      changeLoading(false);
      btnLoading.value = false;
    });
  const handleSubmit = useThrottleFn(doSubmit, 1000);
  async function doSubmit(hasNext: boolean) {
    try {
      changeLoading(true);
      btnLoading.value = true;
      const values = await validate();
      if (isUpdate.value) {
        const res = await defHttp.post({ url: '/admin/station/edit', data: values });
        createMessage.success(res);
      } else {
        const res = await defHttp.post({ url: '/admin/station/add', data: values });
        createMessage.success(res);
      }
      emit('success');
      closeModal();
    } finally {
      btnLoading.value = false;
      changeLoading(false);
    }
  }
  </script>
  